<!-- 友链编辑弹窗 -->
<template>
  <el-dialog :title="isUpdate ? '修改公告' : '添加公告'" :visible="visible" width="750px" :destroy-on-close="true"
    :lock-scroll="false" :close-on-click-modal="false" :before-close="confirmClose" @update:visible="updateVisible">
    <el-form ref="form" :model="form" :rules="rules" label-width="82px">
      <el-form-item label="公告名称:" prop="notice_name">
        <el-input :maxlength="10" v-model="form.notice_name" placeholder="请输入活动名称" clearable />
      </el-form-item>

      <el-form-item label="发送方式" prop="type">
        <div class="unitSearch">
          <el-select clearable v-model="form.type" placeholder="发送方式" class="ele-fluid" @change="showReleaseTime">
            <el-option v-for="(value, idx) in open_time" :key="idx" :label="value" :value="parseInt(idx)" />
          </el-select>
        </div>
      </el-form-item>

      <el-form-item label="发送时间:" v-if="showRelease">
        <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" v-model="form.release_time" type="datetime"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="展示时间:" prop="show_time">
        <el-date-picker style="width: 100%" v-model="form.time_range" type="datetimerange"
          value-format="yyyy-MM-dd HH:mm:ss" label-format="yyyy-MM-dd HH:mm:ss" range-separator="~">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="内容:" style="margin-top: 20px" prop="content">
        <el-input :rows="6" clearable type="textarea" :maxlength="400" v-model="form.content" placeholder="请输入内容" />
      </el-form-item>

      <el-form-item label="备注:" prop="notes">
        <el-input v-model="form.notes" placeholder="备注" clearable />
      </el-form-item>

      <el-form-item label="是否启用:">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="2">否</el-radio>
        </el-radio-group>
      </el-form-item>

    </el-form>
    <div slot="footer">
      <el-button @click="confirmClose()">取消</el-button>
      <el-button type="primary" @click="save" :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import axios from "axios";


export default {
  name: "NoticEdit",
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },

  data() {
    return {

      open_time: {
        1: '实时发送',
        2: '定时发送',
      },
      showRelease: true,
      // 表单数据
      form: Object.assign(
        {
          member_id: "",
          source: "",
          title: "",
          content: "",
          // system_id: ""
        },
        this.data
      ),
      // 表单验证规则
      rules: {
        notice_name: [
          { required: true, message: "请填写公告名称", trigger: "blur" }
        ],
        release_time: [
          { required: true, message: "请选择发布时间", trigger: "blur" }
        ],
        content: [
          { required: true, message: "请输入公告内容", trigger: "blur" }
        ]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,

      //分页功能
      pageSize: 15, //每页显示条数  6条刚好
      currentPage: 1, //当前页数
      pageCount: 6, //按钮数，超过时会折叠
      pageTotal: 0, //总条数
      itemlist: []
    };
  },


  watch: {
    data() {

      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.$set(this.form, 'time_range', [this.$util.toDateString(this.form.start_time), this.$util.toDateString(this.form.end_time)])
        if (this.form.type == 1) {
          this.showRelease = false
        } else {
          this.showRelease = true
        }
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {

    showReleaseTime() {
      if (this.form.type == 1) {
        this.showRelease = false;
      } else {
        this.showRelease = true;
      }
    },

    /* 保存编辑 */
    save() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          this.$http
            .post("/BoxNotify/edit", this.form)
            .then(res => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                if (!this.isUpdate) {
                  this.form = {};
                }
                this.updateVisible(false);
                this.$emit("done");
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(e => {
              this.loading = false;
              this.$message.error(e.message);
            });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit("update:visible", value);
    },

    changeNumber(val) {
      axios.post("/BoxNotify/index", { page: val, limit: this.pageSize }).then(res => {
        this.itemlist = res.data.data;
      });
    },

    confirmClose() {
      this.$confirm("确定要返回吗？", "温馨提示")
        .then(() => {
          this.updateVisible(false);
          if (!this.isUpdate) {
            this.form = {};
          }
        })
        .catch(() => { });
    }
  }
};
</script>

<style scoped></style>
